package com.yootk.provider.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
@Component
@Slf4j
public class SentinelRequestOriginParser implements RequestOriginParser { // 请求解析
    @Override
    public String parseOrigin(HttpServletRequest request) {
        String serviceName = request.getParameter("serviceName"); // 接收请求参数
        if (serviceName == null || "".equals(serviceName)) {    // 参数的内容是空
            serviceName = request.getHeader("serviceName"); // 通过头信息传递参数
        }
        log.info("授权规则的信息，serviceName = " + serviceName);
        if (StringUtils.isEmpty(serviceName)) {
            return request.getRemoteAddr(); // 根据IP地址处理
        }
        return serviceName;
    }
}
